package org.jeecg.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.wms.entity.FinishedGoodsInventoryLog;
import org.jeecg.modules.wms.mapper.FinishedGoodsInventoryLogMapper;
import org.jeecg.modules.wms.service.IFinishedGoodsInventoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 成品库存变动记录Service实现类
 */
@Service
public class FinishedGoodsInventoryLogServiceImpl extends ServiceImpl<FinishedGoodsInventoryLogMapper, FinishedGoodsInventoryLog> 
        implements IFinishedGoodsInventoryLogService {

    @Autowired
    private FinishedGoodsInventoryLogMapper inventoryLogMapper;

    @Override
    public IPage<FinishedGoodsInventoryLog> queryInventoryLogList(IPage<FinishedGoodsInventoryLog> page, 
                                                                 String inventoryId, 
                                                                 String orderNo, 
                                                                 Integer operationType,
                                                                 String keyword) {
        return inventoryLogMapper.queryInventoryLogList((Page<FinishedGoodsInventoryLog>) page, 
                                                       inventoryId, orderNo, operationType, keyword);
    }

    @Override
    public List<FinishedGoodsInventoryLog> queryLogsByInventoryId(String inventoryId) {
        return inventoryLogMapper.queryLogsByInventoryId(inventoryId);
    }

    @Override
    public void recordInventoryChange(String inventoryId, String orderNo, Integer operationType, 
                                    Integer operationQuantity, Integer beforeQuantity, Integer afterQuantity,
                                    String operationReason, String relatedDocNo, String operatorName) {
        FinishedGoodsInventoryLog log = new FinishedGoodsInventoryLog();
        log.setInventoryId(inventoryId);
        log.setOrderNo(orderNo);
        log.setOperationType(operationType);
        log.setOperationQuantity(operationQuantity);
        log.setBeforeQuantity(beforeQuantity);
        log.setAfterQuantity(afterQuantity);
        log.setOperationReason(operationReason);
        log.setRelatedDocNo(relatedDocNo);
        log.setCreateTime(new Date());

        // 获取当前操作员信息
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                String token = request.getHeader("X-Access-Token");
                if (token != null) {
                    String username = JwtUtil.getUsername(token);
                    log.setOperatorId(username);
                    if (operatorName == null) {
                        log.setOperatorName(username);
                    } else {
                        log.setOperatorName(operatorName);
                    }
                }
            }
        } catch (Exception e) {
            // 忽略获取操作员信息的异常
            if (operatorName != null) {
                log.setOperatorName(operatorName);
            }
        }

        this.save(log);
    }
}
